<?php

/**
 * custom group backend 
 *
 */

/**
 * Class for group management in a SQL Database (e.g. MySQL, SQLite)
 */
class OC_Group_Custom extends OC_Group_Backend {

   /**
    * @brief Try to create a new group
    * @param string $gid The name of the group to create
    * @return bool
    *
    * Tries to create a new group. If the group name already exists, false will
    * be returned.
    */
   public function createGroup( $gname ) {
      return true; 
   }

   /**
    * @brief delete a group
    * @param string $gid gid of the group to delete
    * @return bool
    *
    * Deletes a group and removes it from the group_user-table
    */
   public function deleteGroup( $gid ) {
      return true; 
   }

   /**
    * @brief is user in group?
    * @param string $uid uid of the user
    * @param string $gid gid of the group
    * @return bool
    *
    * Checks whether the user is member of a group or not.
    */
   public function inGroup( $uid, $group ) {
    if(empty($group))
         return false;

      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();

      $query = "SELECT COUNT(*) FROM *PREFIX*group_user_custom AS a " .
               " LEFT JOIN *PREFIX*groups_custom AS b ON b.gid=a.gid " .
               " WHERE a.uid=" . $db->quote($uid) . " AND b.groupname=" . $db->quote($group) .
               " AND (b.owner='' OR b.owner=" . $db->quote($user->id) . ")";

      $db->setQuery(PUtil::replaceOCPrefix($query));
      $result = $db->loadResult();
      return $result > 0 ? true: false;
   }

   /**
    * @brief Add a user to a group
    * @param string $uid Name of the user to add to group
    * @param string $gid Name of the group in which add the user
    * @return bool
    *
    * Adds a user to a group.
    */
   public function addToGroup( $uid, $gid ) {
      return false; 
   }

   /**
    * @brief Removes a user from a group
    * @param string $uid Name of the user to remove from group
    * @param string $gid Name of the group from which remove the user
    * @return bool
    *
    * removes the user from a group.
    */
   public function removeFromGroup( $uid, $gid ) {
      return false; 
   }

   /**
    * @brief Get all groups a user belongs to
    * @param string $uid Name of the user
    * @return array with group names
    *
    * This function fetches all groups a user belongs to. It does not check
    * if the user exists at all.
    */
   public function getUserGroups( $uid ) {
      $user = PFactory::getMasterUser();

      $db = PFactory::getDbo();

      $query = "SELECT b.groupname FROM *PREFIX*group_user_custom AS a " .
               " LEFT JOIN *PREFIX*groups_custom AS b ON b.gid=a.gid" .
               " WHERE a.uid=" . $db->quote($uid) . " AND b.owner=" . $db->quote($user->id) .
               " ORDER BY b.isadmin DESC, b.groupname ASC";

      $db->setQuery(PUtil::replaceOCPrefix($query));
      return $db->loadAssocList(null, "groupname");
   }

   /**
    * @brief get a list of all groups
    * @param string $search
    * @param int $limit
    * @param int $offset
    * @return array with group names
    *
    * Returns a list with all groups
    */
   public function getGroups($search = '', $limit = null, $offset = null) {
      if(!$search)
         return null;

      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();

      $query = "SELECT groupname FROM *PREFIX*groups_custom " .
               " WHERE (owner='' or owner=" . $db->quote($user->id)  . ')' .
               " AND groupname LIKE " . $db->quote('%'. $search . '%') .
               " ORDER BY CASE WHEN groupname LIKE " . $db->quote($search . ' %') . " THEN 0
                              WHEN groupname LIKE " . $db->quote($search .'%') . " THEN 1
                              WHEN groupname LIKE " . $db->quote('% ' . $search . '%') . " THEN 2
                              ELSE 3
                         END, isadmin DESC, groupname ASC " . 
               (!is_null($limit) && $limit > 0 ? " LIMIT " . ($offset ? intval($offset) : 0) . ", " .  intval($limit) : "");

      $db->setQuery(PUtil::replaceOCPrefix($query));
      return $db->loadAssocList(null, "groupname");
   }

   /**
    * check if a group exists
    * @param string $gname
    * @return bool
    */
   public function groupExists($gname) {
      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();

      $query = "SELECT count(*) FROM *PREFIX*groups_custom WHERE groupname=" . $db->quote($gname) .
               " AND (owner='' or owner=" . $db->quote($user->id)  . ')'; 
      $db->setQuery(PUtil::replaceOCPrefix($query));

      $result = $db->loadResult();
      return $result > 0 ? true: false;
   }

   /**
    * @brief get a list of all users in a group
    * @param string $gname group name
    * @param string $search
    * @param int $limit
    * @param int $offset
    * @return array with user ids
    */
   public function usersInGroup($gname, $search = '', $limit = null, $offset = null) {
      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();

      $query = "SELECT a.uid FROM *PREFIX*group_user_custom AS a " .
               " LEFT JOIN *PREFIX*groups_custom AS b ON b.gid=a.gid " .
               " LEFT JOIN *PREFIX*users AS u ON u.uid=a.uid " .
               " WHERE b.groupname=" . $db->quote($gname) .
               " AND (b.owner='' OR b.owner=" . $db->quote($user->id) . ")" . 
               " AND u.displayname LIKE " . $db->quote($search . '%') . 
               (!is_null($limit) && $limit > 0 ? " LIMIT " . ($offset ? intval($offset) : 0) . ", " .  intval($limit) : "");


      $db->setQuery(PUtil::replaceOCPrefix($query));
      return $db->loadAssocList(null, 'uid'); 
   }

   /**
    * @brief get a list of all display names in a group
    * @param string $gid
    * @param string $search
    * @param int $limit
    * @param int $offset
    * @return array with display names (value) and user ids (key)
    */
   public function displayNamesInGroup($gname, $search = '', $limit = -1, $offset = 0) {
      $user = PFactory::getMasterUser();
      $db = PFactory::getDbo();

      $query = "SELECT a.uid, u.displayname FROM *PREFIX*group_user_custom AS a " .
               " LEFT JOIN *PREFIX*groups_custom AS b ON b.gid=a.gid " .
               " LEFT JOIN *PREFIX*users AS u ON u.uid=a.uid " .
               " WHERE b.groupname=" . $db->quote($group) .
               " AND (b.owner='' OR b.owner=" . $db->quote($user->id) . ")" .
               " AND u.displayname LIKE " . $db->quote($search . '%') .
               (!is_null($limit) && $limit > 0 ? " LIMIT " . ($offset ? intval($offset) : 0) . ", " .  intval($limit) : "");
 
      $db->setQuery(PUtil::replaceOCPrefix($query));
      return $db->loadAssocList('uid', 'displayname'); 
   }
}
